var ns, toast, actionsheet, UIAlbumBrowser;
// Vue.component('tree-item', {
//     template: '#item-template',
//     props: {
//         item: {}
//     },
//     data: function() {
//         return {
//             isOpen: true,
//             selecttypeid: app.TypeID
//         }
//     },
//     computed: {
//         isFolder: function() {
//             return this.item.children &&
//                 this.item.children.length
//         }
//     },
//     methods: {
//         toggle: function(item) {
//             if (this.isFolder) {
//                 this.isOpen = !this.isOpen
//             } else {
//                 this.choose_type(item);
//             }
//         },
//         choose_type: function(item) {
//             var that = this;
//             app.TypeID = item.ID;
//             app.TypeName = item.text;
//             setTimeout(function() {
//                 app.is_type_list = false;
//             }, 100)
//         },
//     }
// })
var app = new Vue({
    el: '#app',
    data: {
        form: {
            ID: 0,
            StatusDesc: '',
            action: 'customerserviceprocess',
            issuggestion: 0,
            processType: 1,//1-处理2-完工3-回访
            IsInvalidOrder: false,
            RelationIDList: ''
        },
        IsParentProject: 0,
        FinalServiceTypeID: 1,
        imglist: [],
        imgindex: 0,
        img_count: 0,
        select_imglist: [],
        select_imgindex: 0,
        productlist: [],
        is_type_list: false,
        typelist: [],
        TypeID: 0,
        TypeName: '请选择',
        maxuploadcount: CONFIG.maxuploadcount,
        type:1,//1-处理2-完工3-回访
    },
    methods: {
        // get_data: function() {
        //     var that = this;
        //     ns.post({
        //         keywords: that.keywords,
        //         ID: that.form.ID,
        //         action: 'getorgnizationlist',
        //         ServiceType: 1,
        //         onlyservicetype: 1
        //     }, function(succeed, data, err) {
        //         if (succeed) {
        //             that.FinalServiceTypeID = data.FinalServiceTypeID;
        //             that.list = data.list;
        //             that.typelist = data.typelist;
        //             that.TypeID = data.TypeID;
        //             that.TypeName = data.TypeName;
        //             that.taglist = data.taglist;
        //             that.TagID = data.TagID;
        //             that.TagName = data.TagName;
        //             that.categorylist = data.categorylist;
        //             that.CategoryID = data.CategoryID;
        //             that.CategoryName = data.CategoryName;
        //             that.IsParentProject = data.IsParentProject;
        //         } else {
        //             api.toast({
        //                 msg: err,
        //                 duration: 2000,
        //                 location: 'bottom'
        //             });
        //         }
        //     });
        // },
        do_save: function() {
            var that = this;
            // if (that.form.StatusDesc=="") {
            //     api.toast({
            //         msg: '请填写12记录',
            //         duration: 2000,
            //         location: 'bottom'
            //     });
            //     return;
            // }
            var filelist = [];
            for (var i = 0; i < that.imglist.length; i++) {
                filelist.push(that.imglist[i].url);
            }
            // that.form["items"] = that.productlist;
            that.form.action = 'customerserviceprocess';
            that.form.IsInvalidOrder = that.form.IsInvalidOrder ? 1 : 0;
            ns.post(that.form, function(succeed, data, err) {
                if (succeed) {
                    ns.toast('保存成功');
                    api.sendEvent({
                        name: 'customerservicelist_frm',
                    });
                    api.sendEvent({
                        name: 'reloadservicelist',
                    });
                    api.sendEvent({
                        name: 'updata_task_con',
                        extra: {
                            key1: 'value1'
                        }
                    });
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            }, {
                files: {
                    file: filelist
                },
                toast: true,
                toastmsg: '提交中'
            });
        },
        choose_picture: function() {
            var that = this;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();
            })
        },
        choose_picture_start: function() {
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            UIAlbumBrowser.imagePicker({
                max: that.maxuploadcount - that.imglist.length,
                styles: {
                    bg: '#000000',
                    mark: {
                        position: 'top_right',
                        size: 20
                    },
                    nav: {
                        bg: '#000000',
                        cancelColor: '#fff',
                        cancelSize: 16,
                        nextStepColor: '#7fff00',
                        nextStepSize: 16
                    },
                    thumbnail: { //（可选项）返回的缩略图配置，**建议本图片不要设置过大** 若已有缩略图，则使用已有的缩略图。若要重新生成缩略图，可先调用清除缓存接口api.clearCache()。
                        w: 100, //（可选项）数字类型；返回的缩略图的宽；默认：原图的宽度
                        h: 100 //（可选项）数字类型；返回的缩略图的宽；默认：原图的高度
                    }
                },
                animation: true,
            }, function(ret) {
                if (ret.eventType == 'nextStep') {
                    if (ret.list && ret.list.length > 0) {
                        that.select_imglist = ret.list;
                        that.select_imgindex = 0;
                        that.UIAlbumBrowser_transPath();
                    }
                    UIAlbumBrowser.closePicker();
                }
                if (ret.originalPath && ret.originalPath.length > 0) {
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.originalPath,
                        thumbPath: ret.originalPath
                    });
                    that.imgindex++;
                }
            });
        },
        UIAlbumBrowser_transPath: function() {
            var that = this;
            var item = that.select_imglist[that.select_imgindex];
            UIAlbumBrowser.transPath({
                path: item.path
            }, function(ret, err) {
                if (ret) {
                    that.select_imgindex++;
                    if (that.select_imgindex < that.select_imglist.length) {
                        that.UIAlbumBrowser_transPath();
                    } else {
                        that.select_imgindex = 0;
                    }
                    that.imglist.push({
                        index: that.imgindex,
                        url: ret.path,
                        thumbPath: item.thumbPath
                    });
                    that.imgindex++;
                }
            });
        },
        remove_img: function(index) {
            var that = this;
                    for (var i = 0; i < that.imglist.length; i++) {
                        var item = that.imglist[i];
                        if (item.index == index) {
                            that.imglist.splice(i, 1);
                        }
                    }
        },
        do_check_lingliao: function() {
            var that = this;
            that.form.IsLingLiao = !that.form.IsLingLiao;
        },
        do_add_fee: function() {
            var that = this;
            ns.openWin('renwugongdan_addfee_frm', '有偿收费', {
                id: app.form.ID
            });
        },
        do_add_product: function() {
            var that = this;
            var idlist = [];
            for (var i = 0; i < that.productlist.length; i++) {
                idlist.push(that.productlist[i].ID);
            }
            ns.openWin('chooseckproduct_frm', '选择物料', {
                // exceptids: JSON.stringify(idlist),
            });
        },
        get_product_list: function(list) {
            var that = this;
            that.productlist = that.productlist.concat(eval('(' + list + ')'));
        },
        do_remove_product: function(item) {
            var that = this;
            api.confirm({
                title: '提示',
                msg: '确定删除?',
                buttons: ['确定', '取消']
            }, function(ret, err) {
                if (ret.buttonIndex == 1) {
                    for (var i = 0; i < that.productlist.length; i++) {
                        if (that.productlist[i].ID == item.ID) {
                            that.productlist.splice(i, 1);
                        }
                    }
                }
            });
        },
        open_type_list: function() {
            var that = this;
            that.is_type_list = !that.is_type_list;
        },
        do_select_type: function(type) {
            this.form.ProcessType = type;
        },
        do_select_invalid: function() {
            this.form.IsInvalidOrder = !this.form.IsInvalidOrder;
        }
    }
});

apiready = function() {
    ns = window.Foresight.Util;
    toast = new auiToast();
    actionsheet = new auiActionsheet();
    UIAlbumBrowser = api.require('UIAlbumBrowser');
    app.form.ID = api.pageParam.id || 0;
    app.form.processType=api.pageParam.type;
    app.form.issuggestion = api.pageParam.issuggestion || 0;
    // app.get_data();
    api.addEventListener({
        name: 'do_choose_ckproduct_complete'
    }, function(ret, err) {
        if (ret) {
            app.get_product_list(ret.value.list);
        }
    });
    api.addEventListener({
        name: 'dosaveservicefeedone'
    }, function(ret, err) {
        app.form.RelationIDList = ret.value.IDList;
    });

}
